package codeTopPractice;

public class 合并两个有序链表 {

    public static void main(String[] args) {

    }

    public static ListNode mergeTwoList(ListNode list1, ListNode list2){


        if(list1 == null && list2 == null) return null;
        if(list1 == null ) return list2;
        if(list2 == null ) return list1;

        ListNode res = new ListNode();
        ListNode curr = res;

        while (list1!=null && list2!=null){
            if(list1.val<list2.val){
                curr.next = list1;
                curr = curr.next;
                list1 = list1.next;
            }else {
                curr.next = list2;
                curr = curr.next;
                list2 = list2.next;
            }
        }
        while (list1!=null){
            curr.next = list1;
            curr = curr.next;
            list1 = list1.next;
        }

        while (list2!=null){
            curr.next = list2;
            curr = curr.next;
            list2 = list2.next;
        }

        return res.next;
    }

}
